package PrjHttpUtils

import (
	"dgo/goutils/utils"
	"net/http"
	"strings"
)

func AppendIndexBody(sb *utils.BytesBuilder, caption string, url string) {
	sb.Appendf("<div><a href=\"%s\">%s</a></div>", url, caption)
}

func AppendIndexBodyTarget(sb *utils.BytesBuilder, caption string, url string, target string) {
	if len(target) == 0 {
		target = "blank"
	}
	sb.Appendf("<div><a href=\"%s\" target=\"%s\">%s</a></div>", url, target, caption)
}

func ResponseBodyHtml(resp http.ResponseWriter, body string) {
	resp.Header().Set("Context-Type", "text/html; charset=utf-8")
	resp.Write([]byte(body))
}

func ResponseWarningList(w http.ResponseWriter, lst *utils.SyncCycleList, searchval string) {
	var sb utils.BytesBuilder
	lst.RangeDesc(func(args ...interface{}) bool {
		str := utils.GetStrValue(args[0], "")
		if len(str) == 0 {
			return true
		}

		if len(searchval) == 0 || strings.Contains(str, searchval) {
			sb.Appendf("%s\n", str)
		}
		return true
	})
	ResponseBodyHtml(w, sb.String())

}

func ResponseCycleList(w http.ResponseWriter, lst *utils.SyncCycleList, searchval string, maxn int) {
	var sb utils.BytesBuilder
	n := 0
	lst.RangeDesc(func(args ...interface{}) bool {
		str := utils.GetStrValue(args[0], "")
		if len(str) == 0 {
			return true
		}

		if len(searchval) == 0 || strings.Contains(str, searchval) {
			sb.Appendf("%s\n", str)
			n++
			if maxn != 0 && n >= maxn { // 结束
				return false
			}
		}
		return true
	})
	ResponseBodyHtml(w, sb.String())

}
